package cn.zime.iot.handler;

import cn.zime.iot.service.StudentService;
import cn.zime.iot.service.impl.StudentServiceImpl;
import cn.zime.iot.view.AddStudentView;
import cn.zime.iot.view.MainView;
import cn.zime.iot.view.UpdateStudentView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MainViewHandler implements ActionListener {

    private final MainView mainView;

    public MainView getMainView() {
        return mainView;
    }

    public MainViewHandler(MainView mainView) {
        this.mainView = mainView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("增加".equals(text)) {
            new AddStudentView(mainView);
        }else if ("修改".equals(text)) {
            int[] selectedStudentIds = mainView.getSelectedStudentIds();
            if (selectedStudentIds.length != 1) {
                JOptionPane.showMessageDialog(mainView,"一次只能修改一行!");
                return;
            }
            new UpdateStudentView(mainView,selectedStudentIds[0]);
        }else if ("删除".equals(text)) {
            int[] selectedStudentIds = mainView.getSelectedStudentIds();
            if (selectedStudentIds.length == 0) {
                JOptionPane.showMessageDialog(mainView,"请选择要删除的行!");
                return;
            }
            int option = JOptionPane.showConfirmDialog(mainView, "你确认要删除选择的"
                            + selectedStudentIds.length + "行吗?", "确认删除",
                    JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {//确认
                //执行删除
                StudentService studentService = new StudentServiceImpl();
                boolean deleteResult = studentService.delete(selectedStudentIds);
                if (deleteResult) {
                    //重新加载
                    mainView.reloadTable();
                } else {
                    JOptionPane.showMessageDialog(mainView,"删除失败");
                }
            }
        }else if ("查询".equals(text)) {
            mainView.setPageNow(1);
            mainView.reloadTable();
        }else if ("上一页".equals(text)) {
            mainView.setPageNow(mainView.getPageNow() - 1);
            mainView.reloadTable();
        }else if ("下一页".equals(text)) {
            mainView.setPageNow(mainView.getPageNow() + 1);
            mainView.reloadTable();
        }
    }
}
